package com.demo2do.boyle.backend.web.controller.system;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.system.GeoService;
import com.demo2do.boyle.common.entity.geo.Geo;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;

/**
 * 
 * @author leo
 *
 */
@Controller
@RequestMapping("/geos")
@MenuSetting("menu-system")
public class GeoController {
	
	private static final Logger logger = LoggerFactory.getLogger(GeoController.class);
	
	@Autowired
	private GeoService geoService;
	
    /**
     * list all active geos including provinces and cities
     * 
     * @return 
     */
    @RequestMapping("")
    @MenuSetting("submenu-system-geo-list")
    public ModelAndView list() {
    	
        ModelAndView modelAndView = new ModelAndView("system/geo/geo-list");
        
        List<Geo> geos = geoService.listGeos();
        modelAndView.addObject("geos", geos);
        
        return modelAndView;
    }
    
    /**
     * View geo detail
     * 
     * @param code
     * @return 
     */
    @RequestMapping("/{id}")
    public ModelAndView view(@PathVariable("id") String code) {
    	
    	ModelAndView modelAndView = new ModelAndView("system/geo/geo-view");
    	Geo geo = geoService.load(Geo.class, code);
    	modelAndView.addObject("geo", geo);
    	
    	return modelAndView;
    	
    }
    
    /**
     * display the geo activate panel
     * 
     * @return
     */
    @RequestMapping("/activate")
    public String create() {
        return "system/geo/geo-activate";
    }
    
    /**
     * Search inactive geos by name and hot flag
     * 
     * @param name
     * @param hot
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET, params = "filter")
    public @ResponseBody String onSearch(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "hot", required = false) Boolean hot, Page page) {
    	
    	Result result = new Result();
    	
    	List<Geo> geos = geoService.search(name, hot, page);
    	
    	result.success().data("geos", geos);
    	
    	return JsonUtils.toJsonString(result);
    }
    
    /**
     * activate selected provinces and cities 
     * 
     * 
     */
    @RequestMapping( value = "/activate", method = RequestMethod.POST)
    public @ResponseBody String onActivate(@RequestParam("codes") String[] codes) {
    	
    	Result result = new Result();
    	
    	try {
    		geoService.activate(codes);
    		result.success().message("添加成功");
    	} catch (Exception e) {
    		result.fail().message("添加失败");
    		logger.info("onActivate()", e);
    	}
    	
    	return JsonUtils.toJsonString(result);    	
    	
    }
	
}
